/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rm.entidades;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

/**
 *
 * @author ffbeltran
 */
@Entity
@Table(name = "exp_jugadores")
@NamedQueries({@NamedQuery(name = "Jugadores.findAll", query = "SELECT j FROM Jugadores j"),
@NamedQuery(name = "Jugadores.findById", query = "SELECT j FROM Jugadores j WHERE j.id = :id"),
@NamedQuery(name = "Jugadores.findByNombre", query = "SELECT j FROM Jugadores j WHERE j.nombre = :nombre")})
public class Jugadores implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY )
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "nombre", nullable = false, length = 150)
    private String nombre;
    
    public List<ExParcial> getExperiencia() {
		return experiencia;
	}

	public void setExperiencia(List<ExParcial> experiencia) {
		this.experiencia = experiencia;
	}

	@Basic(optional = false)
    @Column(name = "exp_base", nullable = false)
    private int expBase;
//    @Basic(optional = false)
//    @Column(name = "exp_suma", nullable = false)
//    private int expSuma;
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @OrderBy("partida")
    private List<ExParcial> experiencia;

    public int getExpSuma() {
    	int expSumaTemp = 0;
    	for (int i = 0; i < experiencia.size() -1 ; i++) {
    		expSumaTemp += experiencia.get(i).getExperiencia();
		}
		return expSumaTemp;
	}

	public Jugadores() {
		experiencia = new ArrayList<ExParcial>();
    }

    public Jugadores(Integer id) {
    	this();
        this.id = id;
    }

//    public Jugadores(Integer id, String nombre, int expBase, int expSuma) {
//        this.id = id;
//        this.nombre = nombre;
//        this.expBase = expBase;
//        this.expSuma = expSuma;
//    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getExpBase() {
        return expBase;
    }

    public void setExpBase(int expBase) {
    	this.expBase = expBase;
    }

    public int getExpSumaTemp() {
    	if(!experiencia.isEmpty())
    		return experiencia.get(experiencia.size()-1).getExperiencia();
    	else
    		return 0;
    }

    public void setExpSumaTemp(int expSumaTemp) {
        experiencia.get(experiencia.size()-1).setExperiencia(expSumaTemp);
    }

    public int getExpTotal() {
        return expBase + getExpSuma();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Jugadores)) {
            return false;
        }
        Jugadores other = (Jugadores) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.rm.entidades.Jugadores[id=" + id + "]";
    }

}
